package dao;

import java.util.List;

import idao.IOrderdetailsDAO;
import model.Orderdetails;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class OrderdetailsDAO implements IOrderdetailsDAO {

	@Autowired
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public OrderdetailsDAO() {
		// TODO Auto-generated constructor stub
	}

	public boolean insert(Orderdetails orderdetails) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().save(orderdetails);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	public boolean update(Orderdetails orderdetails) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().update(orderdetails);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	public boolean delete(Orderdetails orderdetails) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().delete(orderdetails);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	public Orderdetails getOrdertailByID(int id) {
		// TODO Auto-generated method stub
		try {
			Orderdetails orderdetails = (Orderdetails) sessionFactory
					.getCurrentSession().get(Orderdetails.class, id);
			return orderdetails;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<Orderdetails> getAll() {
		// TODO Auto-generated method stub
		try {
			Query query = sessionFactory.getCurrentSession().createQuery(
					"from Orderdetails");
			return query.list();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

}
